import _toConsumableArray from "@babel/runtime/helpers/esm/toConsumableArray";
import _objectSpread from "@babel/runtime/helpers/esm/objectSpread2";
import _slicedToArray from "@babel/runtime/helpers/esm/slicedToArray";
import { jsxs as _jsxs, jsx as _jsx } from "react/jsx-runtime";
import { Input, InputNumber } from 'antd-v4';
import useMergedState from "rc-util/es/hooks/useMergedState";
import React from 'react';
/**
 * 数字范围组件
 *
 * @param FieldDigitRangeProps
 */

var FieldDigitRange = function FieldDigitRange(_ref, ref) {
  var text = _ref.text,
      type = _ref.mode,
      render = _ref.render,
      placeholder = _ref.placeholder,
      renderFormItem = _ref.renderFormItem,
      fieldProps = _ref.fieldProps,
      _ref$separator = _ref.separator,
      separator = _ref$separator === void 0 ? '~' : _ref$separator,
      _ref$separatorWidth = _ref.separatorWidth,
      separatorWidth = _ref$separatorWidth === void 0 ? 30 : _ref$separatorWidth;
  var value = fieldProps.value,
      defaultValue = fieldProps.defaultValue,
      onChange = fieldProps.onChange,
      id = fieldProps.id;

  var _useMergedState = useMergedState(function () {
    return defaultValue;
  }, {
    value: value,
    onChange: onChange
  }),
      _useMergedState2 = _slicedToArray(_useMergedState, 2),
      valuePair = _useMergedState2[0],
      setValuePair = _useMergedState2[1];

  if (type === 'read') {
    var getContent = function getContent(number) {
      var _fieldProps$formatter;

      var digit = new Intl.NumberFormat(undefined, _objectSpread({
        minimumSignificantDigits: 2
      }, (fieldProps === null || fieldProps === void 0 ? void 0 : fieldProps.intlProps) || {})).format(Number(number));
      return (fieldProps === null || fieldProps === void 0 ? void 0 : (_fieldProps$formatter = fieldProps.formatter) === null || _fieldProps$formatter === void 0 ? void 0 : _fieldProps$formatter.call(fieldProps, digit)) || digit;
    };

    var dom = _jsxs("span", {
      ref: ref,
      children: [getContent(text[0]), " ", separator, " ", getContent(text[1])]
    });

    if (render) {
      return render(text, _objectSpread({
        mode: type
      }, fieldProps), dom);
    }

    return dom;
  }

  if (type === 'edit' || type === 'update') {
    var handleGroupBlur = function handleGroupBlur() {
      if (Array.isArray(valuePair)) {
        //   仅在两个值均为数字时才做比较并转换
        var _valuePair = _slicedToArray(valuePair, 2),
            value0 = _valuePair[0],
            value1 = _valuePair[1];

        if (typeof value0 === 'number' && typeof value1 === 'number' && value0 > value1) {
          setValuePair([value1, value0]);
        } else if (value0 === undefined && value1 === undefined) {
          // 当两个值均为undefined时将值变为undefined，方便required处理
          setValuePair(undefined);
        }
      }
    };

    var handleChange = function handleChange(index, changedValue) {
      var newValuePair = _toConsumableArray(valuePair || []);

      newValuePair[index] = changedValue === null ? undefined : changedValue;
      setValuePair(newValuePair);
    };

    var placeholderValue = (fieldProps === null || fieldProps === void 0 ? void 0 : fieldProps.placeholder) || placeholder;

    var _dom = _jsxs(Input.Group, {
      compact: true,
      onBlur: handleGroupBlur,
      children: [_jsx(InputNumber, _objectSpread(_objectSpread({}, fieldProps), {}, {
        placeholder: Array.isArray(placeholderValue) ? placeholderValue[0] : placeholderValue,
        id: id !== null && id !== void 0 ? id : "".concat(id, "-0"),
        style: {
          width: "calc((100% - ".concat(separatorWidth, "px) / 2)")
        },
        value: valuePair === null || valuePair === void 0 ? void 0 : valuePair[0],
        defaultValue: defaultValue === null || defaultValue === void 0 ? void 0 : defaultValue[0],
        onChange: function onChange(changedValue) {
          return handleChange(0, changedValue);
        }
      })), _jsx(Input, {
        style: {
          width: separatorWidth,
          textAlign: 'center',
          borderLeft: 0,
          borderRight: 0,
          pointerEvents: 'none',
          backgroundColor: '#FFF'
        },
        placeholder: separator,
        disabled: true
      }), _jsx(InputNumber, _objectSpread(_objectSpread({}, fieldProps), {}, {
        placeholder: Array.isArray(placeholderValue) ? placeholderValue[1] : placeholderValue,
        id: id !== null && id !== void 0 ? id : "".concat(id, "-1"),
        style: {
          width: "calc((100% - ".concat(separatorWidth, "px) / 2)"),
          borderLeft: 0
        },
        value: valuePair === null || valuePair === void 0 ? void 0 : valuePair[1],
        defaultValue: defaultValue === null || defaultValue === void 0 ? void 0 : defaultValue[1],
        onChange: function onChange(changedValue) {
          return handleChange(1, changedValue);
        }
      }))]
    });

    if (renderFormItem) {
      return renderFormItem(text, _objectSpread({
        mode: type
      }, fieldProps), _dom);
    }

    return _dom;
  }

  return null;
};

export default /*#__PURE__*/React.forwardRef(FieldDigitRange);